package com.whnk.attendance.service.impl;

import com.whnk.attendance.domain.PmProInfo;
import com.whnk.attendance.mapper.PmProInfoMapper;
import com.whnk.attendance.service.IPmProInfoService;
import com.whnk.attendance.vo.*;
import com.whnk.common.core.exception.ServiceException;
import com.whnk.common.core.utils.DateUtils;
import com.whnk.common.core.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目基本信息Service业务层处理
 *
 * @author whnk
 * @date 2021-11-17
 */
@Service
public class PmProInfoServiceImpl implements IPmProInfoService
{
    @Autowired
    private PmProInfoMapper pmProInfoMapper;

    /**
     * 查询项目基本信息
     *
     * @param proId 项目基本信息主键
     * @return 项目基本信息
     */
    @Override
    public PmProInfo selectPmProInfoByProId(Long proId)
    {
        return pmProInfoMapper.selectPmProInfoByProId(proId);
    }

    /**
     * 查询项目基本信息列表
     *
     * @param pmProInfo 项目基本信息
     * @return 项目基本信息
     */
    @Override
    public List<PmProInfo> selectPmProInfoList(PmProInfo pmProInfo)
    {
        return pmProInfoMapper.selectPmProInfoList(pmProInfo);
    }

    /**
     * 新增项目基本信息
     *
     * @param pmProInfo 项目基本信息
     * @return 结果
     */
    @Override
    public int insertPmProInfo(PmProInfo pmProInfo)
    {
        pmProInfo.setCreateTime(DateUtils.getNowDate());
        return pmProInfoMapper.insertPmProInfo(pmProInfo);
    }

    /**
     * 修改项目基本信息
     *
     * @param pmProInfo 项目基本信息
     * @return 结果
     */
    @Override
    public int updatePmProInfo(PmProInfo pmProInfo)
    {
        pmProInfo.setUpdateTime(DateUtils.getNowDate());
        return pmProInfoMapper.updatePmProInfo(pmProInfo);
    }

    /**
     * 批量删除项目基本信息
     *
     * @param proIds 需要删除的项目基本信息主键
     * @return 结果
     */
    @Override
    public int deletePmProInfoByProIds(Long[] proIds)
    {
        return pmProInfoMapper.deletePmProInfoByProIds(proIds);
    }

    /**
     * 删除项目基本信息信息
     *
     * @param proId 项目基本信息主键
     * @return 结果
     */
    @Override
    public int deletePmProInfoByProId(Long proId)
    {
        return pmProInfoMapper.deletePmProInfoByProId(proId);
    }

    /**
     * 分页查询项目列表
     */
    public List<PmProInfoListVo> selectPmProInfoListVoList(PmProInfoQueryVo pmProInfoQueryVo){
        return pmProInfoMapper.selectPmProInfoListVoList(pmProInfoQueryVo);
    }

    /**
     * 根据逻辑主键查询项目信息
     */
    public PmProInfoListVo selectPmProInfoListVoByProId(Long proId){
        return pmProInfoMapper.selectPmProInfoListVoByProId(proId);
    }

    /**
     * 根据项目编号查询项目信息
     */
    public PmProInfoListVo selectPmProInfoListVoByProNum(String proNum){
        return pmProInfoMapper.selectPmProInfoListVoByProNum(proNum);
    }

    /**
     * 校验项目编号是否唯一
     */
    public boolean checkProNum(PmProInfoUpdateVo pmProInfoUpdateVo){
        PmProInfoListVo info = pmProInfoMapper.checkProNum(pmProInfoUpdateVo.getProNum());
        if(StringUtils.isNotNull(info) && info.getProId() != pmProInfoUpdateVo.getProId()){
            return false;
        }
        return true;
    }

    /**
     * 新增项目信息
     */
    public int insertPmProInfo(PmProInfoUpdateVo pmProInfoUpdateVo){
        PmProInfo pmProInfo = new PmProInfo();
        pmProInfo.setProId(pmProInfoUpdateVo.getProId());
        pmProInfo.setProNum(pmProInfoUpdateVo.getProNum());
        pmProInfo.setProName(pmProInfoUpdateVo.getProName());
        pmProInfo.setProClaNum(pmProInfoUpdateVo.getProClaNum());
        pmProInfo.setProState(pmProInfoUpdateVo.getProState());
        pmProInfo.setCreateTime(DateUtils.getNowDate());
        return pmProInfoMapper.insertPmProInfo(pmProInfo);
    }

    /**
     * 修改项目信息
     */
    public int updatePmProInfo(PmProInfoUpdateVo pmProInfoUpdateVo){
        PmProInfo pmProInfo = new PmProInfo();
        pmProInfo.setProId(pmProInfoUpdateVo.getProId());
        pmProInfo.setProNum(pmProInfoUpdateVo.getProNum());
        pmProInfo.setProName(pmProInfoUpdateVo.getProName());
        pmProInfo.setProClaNum(pmProInfoUpdateVo.getProClaNum());
        pmProInfo.setProState(pmProInfoUpdateVo.getProState());
        pmProInfo.setUpdateTime(DateUtils.getNowDate());
        return pmProInfoMapper.updatePmProInfo(pmProInfo);
    }

    /*
     * 导入项目信息
     */
    public String importData(List<PmProInfoListVo> pmProInfoList, Boolean isUpdateSupport){
        if (StringUtils.isNull(pmProInfoList) || pmProInfoList.size() == 0)
        {
            throw new ServiceException("导入项目信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PmProInfoListVo pmProInfoListVo : pmProInfoList)
        {
            try
            {
                // 验证是否存在这个项目信息
                PmProInfoListVo pmPro = pmProInfoMapper.selectPmProInfoListVoByProNum(pmProInfoListVo.getProNum());
                if (StringUtils.isNull(pmPro))
                {
                    PmProInfoUpdateVo pmProInfoUpdateVo = new PmProInfoUpdateVo();
                    pmProInfoUpdateVo.setProNum(pmProInfoListVo.getProNum());
                    pmProInfoUpdateVo.setProName(pmProInfoListVo.getProName());
                    pmProInfoUpdateVo.setProClaNum(pmProInfoListVo.getProClaNum());
                    pmProInfoUpdateVo.setProState(pmProInfoListVo.getProState());
                    this.insertPmProInfo(pmProInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目信息 " + pmProInfoListVo.getProNum() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    PmProInfoUpdateVo pmProInfoUpdateVo = new PmProInfoUpdateVo();
                    //为修改对象的项目主键赋值
                    pmProInfoUpdateVo.setProId(pmPro.getProId());
                    pmProInfoUpdateVo.setProNum(pmProInfoListVo.getProNum());
                    pmProInfoUpdateVo.setProName(pmProInfoListVo.getProName());
                    pmProInfoUpdateVo.setProClaNum(pmProInfoListVo.getProClaNum());
                    pmProInfoUpdateVo.setProState(pmProInfoListVo.getProState());
                    this.updatePmProInfo(pmProInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目信息 " + pmProInfoListVo.getProNum() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、项目信息 " + pmProInfoListVo.getProNum() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、项目信息 " + pmProInfoListVo.getProNum() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 项目编号id，项目名称name接口
     */
    public List<PmProInfoListVo> proInfoList(){
        return pmProInfoMapper.proInfoList();
    }


//    /**
//     * 新增修改界面项目分类接口
//     */
//    public List<PmProInfoProClaNumVo> selectPmProInfoProClaNumVoList(){
//        return pmProInfoMapper.selectPmProInfoProClaNumVoList();
//    }

//    @Override
//    public List<MonthEmpStatisProNumVo> selectPmProInfoQueryList() {
//        List<PmProInfo> pmSignInfos = pmProInfoMapper.selectPmProInfoQueryList();
//        List<MonthEmpStatisProNumVo> pmSignListVos = pmSignInfos.stream().map(pmSignInfo -> {
//            MonthEmpStatisProNumVo pmSignListVo = new MonthEmpStatisProNumVo();
//            BeanUtils.copyProperties(pmSignInfo, pmSignListVo);
//            return pmSignListVo;
//        }).collect(Collectors.toList());
//        return pmSignListVos;
//    }
}
